package com.gitee.wsl.model.process.response

import com.gitee.wsl.model.process.call.ProcessClientCall
import com.gitee.wsl.model.process.request.ProcessRequest
import kotlinx.coroutines.CompletableJob
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlin.time.ExperimentalTime
import kotlin.time.Instant

/**
 * An [HttpClient]'s response, a second part of [ProcessClientCall].
 *
 * Learn more from [Receiving responses](https://ktor.io/docs/response.html).
 */
 @OptIn(ExperimentalTime::class)
 abstract class ProcessResponse : CoroutineScope {
    /**
     * The associated [ProcessClientCall] containing both
     * the underlying [ProcessClientCall.request] and [ProcessClientCall.processResponse].
     */
     abstract val call: ProcessClientCall

    /**
     * The [HttpStatusCode] returned by the server. It includes both,
     * the [HttpStatusCode.description] and the [HttpStatusCode.value] (code).
     */
     abstract val status: ProcessStatusCode

    /**
     * HTTP version. Usually [HttpProtocolVersion.HTTP_1_1] or [HttpProtocolVersion.HTTP_2_0].
     */
     //abstract val version: HttpProtocolVersion

    /**
     * [GMTDate] of the request start.
     */
     abstract val requestTime: Instant

    /**
     * [GMTDate] of the response start.
     */
     abstract val responseTime: Instant

    /**
     * Provides a raw [ByteReadChannel] to the response content as it is read from the network.
     * This content can be still compressed or encoded.
     *
     * This content doesn't go through any interceptors from [HttpResponsePipeline].
     *
     * If you need to read the content as decoded bytes, use the [bodyAsChannel] method instead.
     *
     * This property produces a new channel every time it's accessed.
     */
     //abstract val rawContent: ByteReadChannel

    override fun toString(): String = "HttpResponse[${request}]"
}


/**
 * Gets [ProcessRequest] associated with this response.
 */
val ProcessResponse.request: ProcessRequest get() = call.request


@PublishedApi
internal fun ProcessResponse.complete() {
    val job = coroutineContext[Job]!! as CompletableJob
    job.complete()
}
